<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-04-04
 * Time: 11:48
 */
namespace app\common\model;
use think\Model;

class UserMoney extends Model{
    // 开启自动写入时间戳
    protected $autoWriteTimestamp = true;
    protected $insert = ['addtime','addip'];
    protected $type = [
        'addtime'    => 'timestamp',
    ];
// 定义时间戳字段名
    protected $createTime = 'addtime';
    protected $updateTime = false;
    protected function setAddipAttr(){
        return request()->ip();
    }
    // 0 => '人民币', 1 => '积分', 2 => '虚拟币'
    // 启用状态
    protected function getAddcontzhAttr($value,$data){
        $addcont = [ 0 => '已提现', 1 => '一级推广', 2 => '二级推广', 3 => '一级返现', 4 => '二级返现', 5 => '申请提现',6 => '注册赠送',7 => '交易扣除',8 => '充值',9 => '课时费',10 => '申请佣金提现',10 => '佣金提现成功'];
        return $addcont[$data['addcont']];
    }
    /**处理老师课时费**/
    public function add_make($data){
        $makedata = array(
            'uid' => $data['uid'],
            'fuid' => '0',
            'addcont' => '9',
            'money_class' => '0',
            'cont' => $data['cont'],
        );
        $makedata = $this->adduser($makedata,$data['money']);
        $this->setAttr('id',null)->isUpdate(false)->save($makedata);
        return true;
    }
    /**处理老师提现**/
    public function add_put($data){
        $putdata = array(
            'uid' => $data['uid'],
            'fuid' => '0',
            'addcont' => $data['addcont'],
            'money_class' => '0',
            'cont' => $data['cont'],
        );
        $putdata = $this->adduser($putdata,$data['money']);
        if($this->setAttr('id',null)->isUpdate(false)->save($putdata)){
            return true;
        }
        return false;
    }
    /**
     * 查询用户余额积分等信息
     */
    public function get_money($data){
        $usermoney = $this->where('uid',$data['uid'])->where('money_class',$data['money_class'])->order('id desc')->find();
        if(empty($usermoney)){
            return false;
        }
        return $usermoney->toArray();
    }
    /**
     * 增加用户推广积分
     * @param $data uid 新注册uid puid为一级推广uid euid为二级推广uid
     * @return bool
     */
    public function add_tui($data){
        $webdb = webdb();
        //  处理一级推广
        switch ($webdb['spread_money_class']){
            case '0':
                $money_class = '元';
                break;
            case '1':
                $money_class = '积分';
                break;
            case '2':
                $money_class = $webdb['moneyname'];
                break;
        }
        if(empty($webdb['user_spread']) || $webdb['user_spread'] == '0'){
            return true;
        }
        $pudata = array(
            'uid' => $data['puid'],
            'fuid' => $data['uid'],
            'addcont' => '1',
            'money_class' => $webdb['spread_money_class'],
            'cont' => "推广新用户增加{$webdb['user_spread']}{$money_class}",
        );
        $pudata = $this->adduser($pudata,$webdb['user_spread']);
        $this->setAttr('id',null)->isUpdate(false)->save($pudata);
        if(!empty($data['euid'])){
            //  瑜伽专用判断
            $egroupid = \model('UserData')->where('uid',$data['euid'])->value('group_id');
            if($egroupid == '6'){
                return true;
            }
            $eudata = array(
                'uid' => $data['euid'],
                'fuid' => $data['uid'],
                'addcont' => '2',
                'money_class' => $webdb['spread_money_class'],
                'cont' => "推广新用户增加{$webdb['user_spread_er']}{$money_class}",
            );
            $eudata = $this->adduser($eudata,$webdb['user_spread_er']);
            $this->setAttr('id',null)->isUpdate(false)->save($eudata);
        }
        return true;
    }
    /**
     * @param $data  接收新注册用户数据 UID 和 MONEY_CLASS
     * @param $addmoney
     * @return bool  返回成功或失败
     */
    public function add_reg($data,$addmoney){
        $webdb = webdb();
        switch ($webdb['user_reg_moneyclass']){
            case '0':
                $money_class = '元';
                break;
            case '1':
                $money_class = '积分';
                break;
            case '2':
                $money_class = $webdb['moneyname'];
                break;
        }
        $addreg = array(
            'uid' => $data['uid'],
            'fuid' => '0',
            'addcont' => '6',
            'money_class' => $data['money_class'],
            'cont' => "会员注册，赠送{$addmoney}{$money_class}",
        );
        $addreg = $this->adduser($addreg,$addmoney);
        if($this->setAttr('id',null)->isUpdate(false)->save($addreg)){
            return true;
        }
        return false;
    }
    /**
     * 用户交易处理
     */
    public function add_pay($data,$addmoney){
        $webdb = webdb();
        switch ($data['money_class']){
            case '0':
                $money_class = '元';
                break;
            case '1':
                $money_class = '积分';
                break;
            case '2':
                $money_class = $webdb['moneyname'];
                break;
        }
        $addpay = array(
            'uid' => $data['uid'],
            'fuid' => '0',
            'addcont' => '7',
            'money_class' => $data['money_class'],
            'cont' => "支付订单{$data['oid']}，使用{$addmoney}{$money_class}",
        );
        $addpay = $this->adduser($addpay,$addmoney);
        if($this->setAttr('id',null)->isUpdate(false)->save($addpay)){
            return true;
        }
        return false;
    }
    /**
     * @param $data  需要处理的用户信息
     * @param $addmoney 当前变化积分或其他记分类型
     * @param string $money_class   积分类型 0 为人民币，1为积分，2为虚拟币
     * @return array    返回处理后的数据
     * 本方法不对数据进得保存，仅做处理
     **/
    public function adduser($data,$addmoney){
        unset($olddata);
        $olddata = $this->where('uid',$data['uid'])->where('money_class',$data['money_class'])->order('id','desc')->find();
        if(empty($olddata)){
            $olddata['paymoney'] = $olddata['user_money'] = $olddata['dmoney'] = $olddata['emoney'] = $olddata['dxmoney'] = $olddata['exmoney'] = $olddata['all_money'] = '0';
        }else{
            $olddata = $olddata->toArray();
        }
        switch ($data['addcont']){
            case '0':
                $data['paymoney'] = $olddata['paymoney'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] - $addmoney;
                break;
            case '1':
                $data['dmoney'] = $olddata['dmoney'] + $addmoney;
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '2':
                $data['emoney'] = $olddata['emoney'] + $addmoney;
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '3':
                $data['dxmoney'] = $olddata['dxmoney'] + $addmoney;
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '4':
                $data['exmoney'] = $olddata['exmoney'] + $addmoney;
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '5':
                $data['user_money'] = $olddata['user_money'] - $addmoney;
                $data['paymoney'] = $olddata['paymoney'] + $addmoney;
                break;
            case '6':
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '7':
                $data['paymoney'] = $olddata['paymoney'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] - $addmoney;
                break;
            case '8':
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '9':
                //  课时费
                $data['all_money'] = $olddata['all_money'] + $addmoney;
                $data['user_money'] = $olddata['user_money'] + $addmoney;
                break;
            case '10':
                $data['user_money'] = $olddata['user_money'] - $addmoney;
                $data['paymoney'] = $olddata['paymoney'] + $addmoney;
                break;
        }
        $data['money'] = $addmoney;
        $newdata = array_merge($olddata,$data);
        unset($newdata['id']);
        return $newdata;
    }


}